import { create } from "zustand";

export interface Store {
  token?: string | null;
  userInfo: object | null;
  loading: boolean;
  setLoading: (bool: boolean) => void;
  setToken: (bool: string) => void;
  removeToken: () => void;
  setUserInfo: (userInfo: object | null) => void;
}

const useGlobalStore = create<Store>((set) => ({
  token: null,
  userInfo: {},
  setUserInfo: (userInfo: object | null) => {
    set({ userInfo });
    localStorage.setItem("userInfo", JSON.stringify(userInfo));
  },
  vipInfos: [],
  vipConfigs: [],
  setToken: (token: string) => {
    // eslint-disable-next-line @typescript-eslint/no-unused-expressions
    token && set({ token });
    localStorage.setItem("token", token);
  },
  removeToken: () => {
    set({ token: null });
    localStorage.removeItem("token");
  },
  loading: false,
  setLoading: (loading: boolean) => {
    set({ loading });
  },
}));

export default useGlobalStore;
